package com.apache.website.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.website.entity.Manuscript;
import com.apache.website.entity.ManuscriptContent;
import com.apache.website.manager.ManuscriptManager;

public class ManuscriptManagerImpl implements ManuscriptManager {

	private IDao manuscriptDao;
	private IDao manuscriptContentDao;
	private final String entityName = "com.apache.website.entity.Manuscript";

	public void setManuscriptDao(IDao manuscriptDao) {
		this.manuscriptDao = manuscriptDao;
	}

	public void setManuscriptContentDao(IDao manuscriptContentDao) {
		this.manuscriptContentDao = manuscriptContentDao;
	}

	public String saveInfo(ParamsVo<Manuscript> vo) throws BusinessException {
		Manuscript org = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		org.setManuscriptId(infoId);
		MethodParam param = new MethodParam("Manuscript", cacheKey, "", entityName);
		param.setVaule(org);
		if (manuscriptDao.insert(param)) {
			if ("1".equals(org.getType())) {
				exeManuscriptContent(infoId, String.valueOf(vo.getParams("content")), 1);
			}
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<Manuscript> vo) throws BusinessException {
		Manuscript org = vo.getObj();
		if (Validator.isNotNull(org.getManuscriptId())) {
			String key = Validator.getDefaultStr(vo.getMethodKey(), "Manuscript");
			if ("Manuscript".equals(key)) {
				String cacheKey = "";
				MethodParam param = new MethodParam("Manuscript", cacheKey, "", entityName);
				param.setVaule(org);
				boolean mark = manuscriptDao.edit(param);
				if (mark) {
					if ("1".equals(org.getType())) {
						exeManuscriptContent(org.getManuscriptId(), String.valueOf(vo.getParams("content")), 2);
					}
				}
				return mark;
			} else {
				MethodParam param = new MethodParam(key, "", "", entityName);
				param.setVaule(org);
				boolean mark = manuscriptDao.edit(param);
				return mark;
			}
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<Manuscript> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", "", "", entityName);
		param.setParams("manuscriptId", infoId);
		if ("true".equals(mark))
			param.setDelete(true);
		boolean flag = manuscriptDao.delete(param);
		if (flag) {
			exeManuscriptContent(infoId, "", 3);
		}
		return flag;
	}

	/**
	 * 操做稿件内容表
	 * @param manuscriptId 稿件ID
	 * @param content 稿件内容
	 * @param type 1:add; 2:edit; 3:del
	 * @return
	 */
	private boolean exeManuscriptContent(String manuscriptId, String content, int type) {
		MethodParam param = new MethodParam("", "", "", "com.apache.website.entity.ManuscriptContent");
		ManuscriptContent mc = new ManuscriptContent();
		boolean mark = false;
		switch (type) {
		case 1://新增
			param.setKey("ManuscriptContent");
			mc.setManuscriptId(manuscriptId);
			mc.setManuscriptContent(content);
			param.setVaule(mc);
			mark = manuscriptContentDao.insert(param);
			break;
		case 2://修改
			param.setKey("ManuscriptContent");
			mc.setManuscriptId(manuscriptId);
			mc.setManuscriptContent(content);
			param.setVaule(mc);
			mark = manuscriptContentDao.edit(param);
			break;
		case 3:
			param.setParams("manuscriptId", manuscriptId);
			param.setKey("ById");
			param.setDelete(true);
			mark = manuscriptContentDao.delete(param);
			break;
		}
		return mark;
	}

	public Object execute(ParamsVo<Manuscript> vo) {
		String key = vo.getKey();
		if ("manuscriptContent".equals(key)) {
			return getManuscriptContent(vo.getInfoId());
		} else if ("manuscriptsByChannelId".equals(key)) {
			return countManuscriptsByChannelId(vo.getInfoId());
		}
		return null;
	}

	/**
	 * 获取指定稿件内容
	 * @param manuscriptId 稿件ID
	 * @return
	 */
	private String getManuscriptContent(String manuscriptId) {
		MethodParam param = new MethodParam("ById", "", "", "com.apache.website.entity.ManuscriptContent");
		param.setParams("manuscriptId", manuscriptId);
		return String.valueOf(manuscriptContentDao.selectSingle(param));
	}

	private long countManuscriptsByChannelId(String channelId) {
		MethodParam param = new MethodParam("ByChannelId", "", "", entityName);
		param.setParams("channelId", channelId);
		return manuscriptDao.count(param);
	}

	public Object getInfoById(ParamsVo<Manuscript> vo) {
		String infoId = vo.getInfoId();
		String key = Validator.getDefaultStr(vo.getMethodKey(), "ById");
		String cacheKey = "";
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam(key, cacheKey, "", entityName);
		param.setInfoId(infoId);
		return manuscriptDao.selectById(param);
	}

	public Page getPageInfo(ParamsVo<Manuscript> vo) {
		String orderBy = vo.getKey();
		if (Validator.isNull(orderBy))
			orderBy = "published_Time";
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		param.setOrderby(orderBy + " desc");
		Page page = manuscriptDao.pageSelect(param);
		return page;
	}

	public List<Manuscript> getList(ParamsVo<Manuscript> vo) {
		String orderBy = vo.getKey();
		if (Validator.isNull(orderBy))
			orderBy = "published_Time";
		MethodParam param = setMethodParams(vo, 2);
		param.setOrderby(orderBy + " desc");
		return manuscriptDao.select(param);
	}

	public long countInfo(ParamsVo<Manuscript> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return manuscriptDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<Manuscript> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		Manuscript org = vo.getObj();
		if (Validator.isEmpty(org))
			return param;
		if (Validator.isNotNull(org.getChannelId())) {
			param.setParams("channelId", org.getChannelId());
		}
		if (Validator.isNotNull(org.getType())) {
			param.setParams("type", org.getType());
		}
		if (Validator.isNotNull(org.getChannelCode())) {
			param.setParams("channelCode", org.getChannelCode());
		}
		if (Validator.isNotNull(org.getTitle())) {
			param.setParams("title", "'%" + org.getTitle() + "%'");
		}
		if (Validator.isNotNull(org.getKeyword())) {
			param.setParams("keyword", "'%" + org.getKeyword() + "%'");
		}
		if (Validator.isNotNull(org.getMemo())) {
			param.setParams("memo", "'%" + org.getMemo() + "%'");
		}
		if (Validator.isNotNull(org.getStatus())) {
			param.setParams("status", org.getStatus());
		}
		if (Validator.isNotNull(org.getCreatorId())) {
			param.setParams("creatorId", org.getCreatorId());
		}
		if (Validator.isNotNull(org.getIsQuoted())) {
			param.setParams("isQuoted", org.getIsQuoted());
		}
		if (Validator.isNotNull(org.getIsEncrypted())) {
			param.setParams("isEncrypted", org.getIsEncrypted());
		}
		if (Validator.isNotNull(org.getCreatorId())) {
			param.setParams("creatorId", org.getCreatorId());
		}
		if (Validator.isNotNull(org.getIsPageBreak())) {
			param.setParams("isPageBreak", org.getIsPageBreak());
		}
		return param;
	}
}
